package com.zx.flow.core.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zx.flow.api.config.FlowConfig;
import com.zx.flow.api.execute.model.FlowContext;
import com.zx.flow.api.execute.model.FlowNode;
import com.zx.flow.api.execute.request.FlowParam;
import com.zx.flow.api.execute.request.manager.ManagerFlowConfigListForm;
import com.zx.flow.api.execute.request.manager.ManagerFlowListForm;
import com.zx.flow.api.execute.response.FlowResult;
import com.zx.flow.api.execute.response.Response;
import com.zx.flow.api.execute.response.ResponseStatus;
import com.zx.flow.core.engine.FlowEngine;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 对外暴露业务功能，统一维护事务
 */
@Service
public class FlowServiceImpl implements FlowService {

    @Autowired
    private FlowEngine flowEngine;

    @Override
    public Response<IPage<FlowContext>> getFlowList(ManagerFlowListForm form) {
        return Response.success(this.flowEngine.getStorageHandler().getFlowList(form));
    }

    @Override
    public Response<IPage<FlowConfig>> getFlowConfigList(ManagerFlowConfigListForm form) {
        return Response.success(this.flowEngine.getStorageHandler().getFlowConfigList(form));
    }

    @Override
    public Response<FlowConfig> saveFlowConfig(FlowConfig flowConfig) {
        this.flowEngine.getStorageHandler().saveFlowConfig(flowConfig);
        return Response.success(flowConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response<FlowContext> execute(FlowParam param) {
        return this.convert(this.flowEngine.execute(param));
    }

    @Override
    public Response<FlowContext> getFlow(String flowId) {
        return Response.success(this.flowEngine.getStorageHandler().getFlow(flowId));
    }

    @Override
    public Response<FlowNode> getNode(String flowId, String nodeId) {
        return Response.success(this.flowEngine.getStorageHandler().getNode(flowId, nodeId));
    }

    @Override
    public Response<List<FlowNode>> getNodes(String flowId) {
        return Response.success(this.flowEngine.getStorageHandler().getNodes(flowId));
    }

    /**
     * 响应结果类型转换。 FlowResult -> Response
     *
     * @param flowResult
     * @return
     */
    public Response convert(FlowResult flowResult) {
        if (flowResult.isOk()) {
            return Response.build(ResponseStatus.OK, flowResult.getMsg(), flowResult.getFlowContext());
        } else if (flowResult.isFailed()) {
            return Response.build(ResponseStatus.FLOW_ERROR_801, flowResult.getMsg(), flowResult.getFlowContext());
        } else if (flowResult.isException()) {
            return Response.build(ResponseStatus.FLOW_ERROR_802, flowResult.getMsg(), flowResult.getFlowContext());
        } else {
            return Response.build(ResponseStatus.FLOW_ERROR_999, flowResult.getMsg(), flowResult.getFlowContext());
        }
    }

}
